//
//  WKNiuwaTuwenVC.m
//  V-Learn
//
//  Created by Apple on 2019/3/22.
//  Copyright © 2019 yxk. All rights reserved.
//

#import "WKNiuwaTuwenVC.h"
#import "WKDakaImageViewCell.h"
#import "WKDakaDetailImageTextCell.h"
//#import "VKRecordInfoModel.h"
#import "WKHotclockInfoVOListModel.h"
@interface WKNiuwaTuwenVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIButton *followBtn;
@property (nonatomic, strong) UIButton *zanButton;
@property (nonatomic, strong) NSString *isAtten;/**关注*/
@property (nonatomic, strong) NSString *isPrase;/**赞*/

@property (nonatomic, strong) UIView *footerView;
@property (nonatomic, strong) UIImageView *footImageView;
@property (nonatomic, strong) UIView *headerView;
@property (nonatomic, strong) UIImageView *headerImageView;

@end

@implementation WKNiuwaTuwenVC
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, LBNAVBAR_HEIGHT, ScreenW, ScreenH-LBNAVBAR_HEIGHT) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = self.nameTitle;
    [self.view addSubview:self.tableView];
    self.tableView.tableFooterView = [UIView new];
    self.tableView.estimatedRowHeight = 40;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    if (@available(iOS 11.0, *)) {
        _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }else{
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    //底部控件创建
    [self setupFooterView];
    
    //尾部视图
    self.footerView = [UIView new];
    self.footImageView = [[UIImageView alloc] init];
    [self.footerView addSubview:self.footImageView];
    self.footerView.frame = CGRectMake(0, 0, ScreenW, 1);
    self.footImageView.frame = CGRectMake(0, 0, ScreenW, 1);
    //头部视图
    self.headerView = [UIView new];
    self.headerImageView = [[UIImageView alloc] init];
    [self.headerView addSubview:self.headerImageView];
    self.headerView.frame = CGRectMake(0, 0, ScreenW, 1);
    self.headerImageView.frame = CGRectMake(0, 0, ScreenW, 1);
    //获取头部和尾部视图
    [self requestImagesData];
}
#pragma mark - 获取图片接口
- (void)requestImagesData{
    //    ImageConfigController/queryBackImage
    __weak typeof(self)weakSelf = self;
    [WYHXNetWorkManager get:WKQueryBackImage_WKURL params:@{} completionHandler:^(id responseObj, NSError *error) {
        NSString *code = responseObj[@"code"];
        if ([code integerValue]==200) {
            //            NSLog(@"%@",responseObj);
            NSDictionary *dic = responseObj[@"data"];
            NSString *clockUpImage = dic[@"clockUpImage"];
            NSString *clockDownImage = dic[@"clockDownImage"];
            //            NSString *recommendImage = dic[@"recommendImage"];
            NSString *urlStringUpImage = [NSString stringWithFormat:@"%@%@",Public_URL,clockUpImage];
            NSString *urlStringDownImage = [NSString stringWithFormat:@"%@%@",Public_URL,clockDownImage];
            
            [self.footImageView sd_setImageWithURL:[NSURL URLWithString:urlStringUpImage] placeholderImage:[UIImage imageNamed:@"wk_list_bgimg"] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
                //                CGSize size = image.size;
                //
                //                CGFloat w = size.width;
                //
                //                CGFloat H = size.height;
                
                //                CGFloat scale = H/w;
                CGFloat pHeight = 150;
                CGFloat pWidth = image.size.width * pHeight / image.size.height;
                self.footerView.frame = CGRectMake(0, 0, ScreenW, pWidth);
                self.footImageView.frame = CGRectMake(0, 0, ScreenW, pWidth);
                weakSelf.tableView.tableFooterView = self.footerView;
                
            }];
            [self.headerImageView sd_setImageWithURL:[NSURL URLWithString:urlStringDownImage] placeholderImage:[UIImage imageNamed:@"wk_list_bgimg"] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
                
                CGFloat pHeight = 150;
                CGFloat pWidth = image.size.width * pHeight / image.size.height;
                self.headerView.frame = CGRectMake(0, 0, ScreenW, pWidth);
                self.headerImageView.frame = CGRectMake(0, 0, ScreenW, pWidth);
                weakSelf.tableView.tableHeaderView = self.headerView;
                
            }];
        }
        [self.tableView reloadData];
    }];
}
- (void)setupFooterView{
    
    UIView *bottomView = [UIView new];
    [self.view addSubview:bottomView];
    bottomView.backgroundColor = [UIColor whiteColor];
    [bottomView makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.mas_equalTo(0);
        make.height.mas_equalTo(44*kScaleFit);
    }];
    
    UIView *topLine = [UIView new];
    [bottomView addSubview:topLine];
    topLine.backgroundColor = rgb(195,195,195);
    [topLine makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(0);
        make.height.mas_equalTo(0.5);
    }];
    //中间线
    UIView *lineView = [UIView new];
    [bottomView addSubview:lineView];
    lineView.backgroundColor = rgb(195,195,195);
    [lineView makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(bottomView);
        make.centerX.mas_equalTo(bottomView);
        make.size.mas_equalTo(CGSizeMake(1*kScaleFit, 24*kScaleFit));
    }];
    
    //赞
    self.zanButton = [[UIButton alloc] init];
    [bottomView addSubview:self.zanButton];
    [self.zanButton setTitle:self.model.praiseNum forState:UIControlStateNormal];
    [self.zanButton setTitleColor:rgb(233,84,18) forState:UIControlStateNormal];
    if ([self.model.isPrase isEqualToString:@"1"]) {
        [self.zanButton setImage:LBImage(@"wk_Fabulous") forState:UIControlStateNormal];
    }else{
        [self.zanButton setImage:LBImage(@"wk_unFabulous") forState:UIControlStateNormal];
    }
    
    self.zanButton.titleLabel.font = LBFont(12*kScaleFit);
    [self.zanButton makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.bottom.mas_equalTo(0*kScaleFit);
        make.width.mas_equalTo(ScreenW/2);
    }];
    [self.zanButton addTarget:self action:@selector(clickZanBtn:) forControlEvents:UIControlEventTouchUpInside];
    
    //关注
    self.followBtn = [[UIButton alloc] init];
    [bottomView addSubview:self.followBtn];
    if ([self.isAtten isEqualToString:@"1"]) {
        [self.followBtn setTitle:@"取消关注" forState:UIControlStateNormal];
    }else{
        [self.followBtn setTitle:@"关注" forState:UIControlStateNormal];
    }
    [self.followBtn setTitleColor:rgb(233,84,18) forState:UIControlStateNormal];
    
    self.followBtn.titleLabel.font = LBFont(14*kScaleFit);
    [self.followBtn makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.mas_equalTo(0*kScaleFit);
        make.width.mas_equalTo(ScreenW/2);
    }];
    [self.followBtn addTarget:self action:@selector(clickFollowBtn:) forControlEvents:UIControlEventTouchUpInside];
    self.isAtten = self.model.isAtten;
    self.isPrase = self.model.isPrase;
}
#pragma mark - 关注
- (void)clickFollowBtn:(UIButton *)sender{
    
    NSString *token = [UserModel getUserModel].token;
    if ([self.isAtten isEqualToString:@"1"]) {
        [WYHXNetWorkManager post:WKCancelFollowniuwa_WKURL params:@{@"token":token,@"attenUserId":self.model.userId} completionHandler:^(id responseObj, NSError *error) {
            //            NSLog(@"%@",responseObj);
            NSString *code = responseObj[@"code"];
            if ([code integerValue]==200) {
                self.isAtten = @"0";
                self.model.isAtten = @"0";
                [sender setTitle:@"关注" forState:UIControlStateNormal];
                
            }
        }];
    }else{
        [WYHXNetWorkManager post:WKFollowniuwa_WKURL params:@{@"token":token,@"attenUserId":self.model.userId} completionHandler:^(id responseObj, NSError *error) {
            //            NSLog(@"%@",responseObj);
            NSString *code = responseObj[@"code"];
            if ([code integerValue]==200) {
                
                self.isAtten = @"1";
                 self.model.isAtten = @"1";
                [sender setTitle:@"取消关注" forState:UIControlStateNormal];
            }
        }];
    }
}
#pragma mark - 点赞
- (void)clickZanBtn:(UIButton *)sender{
    NSString *token = [UserModel getUserModel].token;
    if ([self.isPrase isEqualToString:@"1"]) {
        [WYHXNetWorkManager post:WKCancelPraiseClock_WKURL params:@{@"token":token,@"clockId":self.model.clockId} completionHandler:^(id responseObj, NSError *error) {
            //            NSLog(@"%@",responseObj);
            NSString *code = responseObj[@"code"];
            if ([code integerValue]==200) {
                self.isPrase = @"0";
                [sender setImage:[UIImage imageNamed:@"wk_unFabulous"] forState:UIControlStateNormal];
                if (self.model.praiseNum==nil||self.model.praiseNum.length==0||self.model.praiseNum == 0) {
                    self.model.praiseNum = @"0";
                    [sender setTitle:@"0" forState:UIControlStateNormal];
                }else{
                    NSString *zanCount = [NSString stringWithFormat:@"%ld",[self.model.praiseNum integerValue]-1];
                    [sender setTitle:zanCount forState:UIControlStateNormal];
                    self.model.praiseNum = zanCount;
                    self.model.isPrase = @"0";
                }
                
            }
        }];
    }else{
        [WYHXNetWorkManager post:WKPraiseClock_WKURL params:@{@"token":token,@"clockId":self.model.clockId} completionHandler:^(id responseObj, NSError *error) {
            //            NSLog(@"%@",responseObj);
            NSString *code = responseObj[@"code"];
            if ([code integerValue]==200) {
                [sender setImage:[UIImage imageNamed:@"wk_Fabulous"] forState:UIControlStateNormal];
                //刷新数据
                self.isPrase = @"1";
                NSString *zanCount = [NSString stringWithFormat:@"%ld",[self.model.praiseNum integerValue]+1];
                [sender setTitle:zanCount forState:UIControlStateNormal];
                self.model.praiseNum = zanCount;
                self.model.isPrase = @"1";
            }
        }];
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.model.clockInfoVOList.count+1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    /**
     WKHotmenModel *model = self.sourceArray[indexPath.section];
     WKHotclockInfoVOListModel
     */
    if (indexPath.section==0) {
        WKDakaImageViewCell *cell = [WKDakaImageViewCell cellWithTableView:tableView];
//        cell.titleLab.text = self.model.clockTitle;
//        cell.detailLab.text = self.model.clockOrtherTitle;
        cell.model = self.model;
        return cell;
    }else{
        WKDakaDetailImageTextCell *cell = [WKDakaDetailImageTextCell cellWithTableView:tableView];
        WKHotclockInfoVOListModel *model = self.model.clockInfoVOList[indexPath.section-1];
        cell.model = model;
        return cell;
    }
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0) {
        return UITableViewAutomaticDimension;
    }
    //    return 44;
    return UITableViewAutomaticDimension;
}
@end
